package com.atguigu.cmsservice.controller;


import com.atguigu.cmsservice.entity.CrmBanner;
import com.atguigu.cmsservice.service.CrmBannerService;
import com.atguigu.commonutils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author kaixuan
 * @since 2020-06-09
 */
@Api(description = "Cms管理")
@RestController
@RequestMapping("/cmsservice/crmBanner")
//@CrossOrigin
public class CrmBannerController {

    @Autowired
    private CrmBannerService bannerService;

    @ApiOperation("获取Banner分页数据")
    @GetMapping("getNodeList/{curret}/{size}")
    public R nodeList(@ApiParam(value = "每页数",name = "curret",required = true)@PathVariable Long curret,
                      @ApiParam(value = "当前页",name = "size",required = true)@PathVariable Long size)
    {
        Page<CrmBanner> page = new Page<>(curret,size);
        bannerService.page(page,null);
        long total = page.getTotal();
        List<CrmBanner> items = page.getRecords();
        return R.ok().data("items",items).data("total",total);
    }

    @ApiOperation("根据id获取Banner数据")
    @GetMapping("getBanner/{id}")
    public R getBanner(@ApiParam(value = "id",name = "id",required = true)@PathVariable String id)
    {
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("banner",banner);
    }

    @ApiOperation("添加Banner")
    @PostMapping("saveBanner")
    public R saveBanner(@RequestBody CrmBanner crmBanner)
    {
        boolean flag = bannerService.save(crmBanner);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("修改Banner")
    @PostMapping("updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner)
    {
        boolean flag = bannerService.updateById(crmBanner);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("删除Banner")
    @DeleteMapping("{id}")
    public R updateBanner(@ApiParam(value = "id",name = "id",required = true)@PathVariable String id)
    {
        boolean flag = bannerService.removeById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

}

